package spring.transaction;


import java.util.HashMap;
import java.util.Map;

public class TransactionThreadManager {

    private static final ThreadLocal<Map<Object, Object>> resources = new NamedThreadLocal<>("Transactional resources");

    public static Object getResource(Object key) {
        Map<Object, Object> map = resources.get();
        if (map == null) {
            return null;
        }
        return map.get(key);
    }

    public static void bindResource(Object key, Object value) {
        Map<Object, Object> map = resources.get();
        if (map == null) {
            map = new HashMap<>();
            resources.set(map);
        }
        map.put(key, value);
    }

    public static boolean hasResource(Object key) {
        Map<Object, Object> map = resources.get();
        if (map == null) {
            return false;
        }
        return map.containsKey(key);
    }

    public static void releaseResource() {
        resources.remove();
    }

}
